(ns
  com.marzhillstudios.molehill.test-indexer
  (:use [clojure.test]
     [com.marzhillstudios.molehill.indexer]))

; TODO(jwall): handle the new index format
(deftest test-update-index-map
         (let [new-map (update-index-map
                         (mk-index-map)
                         {:key "foo"
                          :value [2 "bar2"]
                          :request :update})]
           (is (= (map second (second (find new-map "foo"))) (sort ["bar2"]))))
         (let [new-map (update-index-map
                         (mk-index-map "foo" [[1 "bar1"]])
                         {:key "foo"
                          :value [2 "bar2"]
                          :request :update})]
           (is (= (map second (second (find new-map "foo"))) (sort ["bar1" "bar2"]))))
         (let [new-map (update-index-map
                         (mk-index-map "foo" [[1 "bar1"]])
                         {:key "foo2"
                          :value [2 "bar2"]
                          :request :update})]
           (is (= (map second (second (find new-map "foo2"))) (sort ["bar2"]))))
         (let [new-map (update-index-map
                         (mk-index-map "foo" [[1 "bar1"] [2 "bar2"]])
                         {:key "foo"
                          :value [2 "bar2"]
                          :request :delete})]
           (is (= (map second (second (find new-map "foo"))) (sort ["bar1"])))))

(deftest test-do-update-index
         (let [index-map (mk-index-map "foo" [[1 "bar1"] [2 "bar2"] [3 "bar3"]])
               new-index (do-update-index
                           index-map
                           [(mk-update-request [4 "bar4"] "foo" :update)
                             (mk-update-request [1 "bar1"] "foo" :delete)])]
           (is (= (map second (second (find new-index "foo"))) (sort ["bar2" "bar3" "bar4"])))))
